package cn.innosmart.aq.reminder;

import android.app.AlarmManager;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

import cn.innosmart.aq.R;
import cn.innosmart.aq.view.activity.ReminderNotificationActivity;

/**
 * 接受备忘提醒到时间点后出发的Service
 * Created by David on 2016/1/18.
 */
public class ReminderService extends IntentService {
    public static final String TITLE = "title";
    public static final String TIME_HOUR = "timeHour";
    public static final String TIME_MINUTE = "timeMinute";
    public static final String CONTENT = "content";
    public static final String ID = "id";
    public static final String PENDINITENT = "pendinitent";
    AlarmManager alarmManager = null;
    PendingIntent alarmIntent = null;
    NotificationManager notificationManager = null;

    public ReminderService() {
        super("NotificationService");
    }

    public ReminderService(String name) {
        super(name);
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        //创建通知提示用户备忘提醒的消息
        String title = intent.getStringExtra(TITLE);
        String content = intent.getStringExtra(CONTENT);
        int count = intent.getIntExtra(PENDINITENT, 1234567);
        int id = intent.getIntExtra(ID, 0);
        //定义notification
        Notification n = new Notification(R.mipmap.ic_launcher, getString(R.string.activity_more_reminder), System.currentTimeMillis());
        n.flags = Notification.FLAG_AUTO_CANCEL;
        n.defaults = Notification.DEFAULT_SOUND;
        Intent it = new Intent(this, ReminderNotificationActivity.class);
        it.putExtra(TITLE, title);
        it.putExtra(CONTENT, content);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pi = PendingIntent.getActivity(this, count, it, PendingIntent.FLAG_CANCEL_CURRENT);
        //具体的通知内容
        n.icon = R.drawable.status_bar_icon;
        n.setLatestEventInfo(this, getString(R.string.activity_more_reminder) + "(" + title + ")", content, pi);
        //执行通知
        notificationManager.notify(id, n);
    }

}
